﻿#region

using System;
using System.Linq;
using System.Web.Mvc;
using HouseHoldBudget.Core.Entities.Incomes;
using HouseHoldBudget.Models.Incomes;
using HouseHoldBudget.Services.Incomes;
using Kendo.Mvc.Extensions;
using Kendo.Mvc.UI;

#endregion

namespace HouseHoldBudget.Controllers
{
    public class IncomesController : BudgetControllerBase
    {
        #region Readonly & Static Fields

        private readonly IFixedIncomesService _fixedIncomeService;

        #endregion

        #region Constructors

        public IncomesController(IFixedIncomesService fixedIncomeService)
        {
            _fixedIncomeService = fixedIncomeService;
        }

        #endregion

        #region Instance Methods

        /// <summary>
        ///   Deletes one income.
        /// </summary>
        /// <param name="request"> The request. </param>
        /// <param name="id"> The id. </param>
        /// <returns> </returns>
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult DeleteFixedIncome([DataSourceRequest] DataSourceRequest request, Guid id)
        {
            _fixedIncomeService.DeleteFixedIncome(id);

            return GetAllFixedIncomesAsJson(request);
        }

        /// <summary>
        ///   Gets the expenses list.
        /// </summary>
        /// <param name="request"> The request. </param>
        /// <returns> </returns>
        public ActionResult GetFixedIncomesList([DataSourceRequest] DataSourceRequest request)
        {
            return GetAllFixedIncomesAsJson(request);
        }

        /// <summary>
        ///   Inserts one income.
        /// </summary>
        /// <param name="request"> The request. </param>
        /// <param name="model"> The model. </param>
        /// <returns> </returns>
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult InsertFixedIncome([DataSourceRequest] DataSourceRequest request, FixedIncomeModel model)
        {
            FixedIncome income = new FixedIncome
                                     {
                                         Id = Guid.NewGuid(),
                                         FixedIncomesAmount = model.FixedIncomesAmount,
                                         FixedIncomesDescription = model.FixedIncomesDescription,
                                         BudgetUserId = CurrentBudgetUser.Id,
                                         FixedIncomesOn = model.FixedIncomesOn,
                                         FixedIncomesName = model.FixedIncomesName
                                     };

            _fixedIncomeService.InsertFixedIncome(income);

            model.Id = income.Id;
            return Json(new[] {model}.ToDataSourceResult(request));
        }

        /// <summary>
        ///   Shows my incomes.
        /// </summary>
        /// <returns> </returns>
        public ActionResult ShowMyIncomes()
        {
            //Get the list of income types
            return View();
        }

        /// <summary>
        ///   Updates one income.
        /// </summary>
        /// <param name="request"> The request. </param>
        /// <param name="model"> The model. </param>
        /// <returns> </returns>
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult UpdateFixedIncome([DataSourceRequest] DataSourceRequest request, FixedIncomeModel model)
        {
            FixedIncome currentIncome = _fixedIncomeService.GetFixedIncomeById(model.Id);
            currentIncome.FixedIncomesAmount = model.FixedIncomesAmount;
            currentIncome.FixedIncomesDescription = model.FixedIncomesDescription;
            currentIncome.FixedIncomesName = model.FixedIncomesName;
            currentIncome.FixedIncomesOn = model.FixedIncomesOn;
            _fixedIncomeService.UpdateFixedIncome(currentIncome);

            return GetAllFixedIncomesAsJson(request);
        }

        /// <summary>
        ///   Gets all fixed incomes as json.
        /// </summary>
        /// <param name="request"> The request. </param>
        /// <returns> </returns>
        [NonAction]
        private JsonResult GetAllFixedIncomesAsJson(DataSourceRequest request)
        {
            var incomes = _fixedIncomeService.GetFixedIncomesByUserId(CurrentBudgetUser.Id);
            return Json(incomes.Select(PrepareIncomeModelForList).ToDataSourceResult(request, ModelState));
        }

        /// <summary>
        ///   Prepares the income model for list.
        /// </summary>
        /// <param name="income"> The income. </param>
        /// <returns> </returns>
        [NonAction]
        private FixedIncomeModel PrepareIncomeModelForList(FixedIncome income)
        {
            var model = new FixedIncomeModel
                            {
                                FixedIncomesAmount = income.FixedIncomesAmount,
                                FixedIncomesDescription = income.FixedIncomesDescription,
                                Id = income.Id,
                                FixedIncomesName = income.FixedIncomesName,
                                FixedIncomesOn = income.FixedIncomesOn
                            };

            return model;
        }

        #endregion
    }
}